unit fServerForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
  uDAPoweredByDataAbstractButton, uROPoweredByRemObjectsButton, TB2Dock,
  TB2Toolbar, SpTBXItem, TB2Item, Menus, ActnList, OmniXmlConf,
  ExtCtrls, RzTray;

type
  TServerForm = class(TForm)
    dkTop: TSpTBXDock;
    tlbMain: TSpTBXToolbar;
    sbmnuFile: TSpTBXSubmenuItem;
    itemExit: TSpTBXItem;
    itemConfig: TSpTBXItem;
    mnuPopup: TSpTBXPopupMenu;
    ActionList1: TActionList;
    actAbout: TAction;
    actExit: TAction;
    actStartService: TAction;
    actStopService: TAction;
    actConfig: TAction;
    itemAbout: TSpTBXItem;
    itemStopService: TSpTBXItem;
    itemStartService: TSpTBXItem;
    SpTBXSeparatorItem2: TSpTBXSeparatorItem;
    tmRefreshUserList: TTimer;
    lblUserCount: TLabel;
    itemWarehouseSnapShot: TSpTBXItem;
    tmWarehouseAnalyse: TTimer;
    SpTBXItem1: TSpTBXItem;
    RzTrayIcon1: TRzTrayIcon;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure actConfigExecute(Sender: TObject);
    procedure actExitExecute(Sender: TObject);
    procedure actStartServiceExecute(Sender: TObject);
    procedure actStopServiceExecute(Sender: TObject);
    procedure actAboutExecute(Sender: TObject);
    procedure tmRefreshUserListTimer(Sender: TObject);
    procedure itemWarehouseSnapShotClick(Sender: TObject);
    procedure tmWarehouseAnalyseTimer(Sender: TObject);
    procedure SpTBXItem1Click(Sender: TObject);
    procedure RzTrayIcon1LButtonDblClick(Sender: TObject);
    procedure RzTrayIcon1RButtonDown(Sender: TObject);
  private
    { Private declarations }
    FYear: Integer;
    FMonth: Integer;
    FIsGenerated: Boolean;
    FServiceRunning: Boolean;
    FCanExit: Boolean;
    procedure GenerateWarehouseAnalyse;
    procedure ShowMenuControl;
    procedure StartService;
    procedure StopService;
    procedure LoadClientFilesVersion;
  public
    { Public declarations }
  end;

var
  ServerForm: TServerForm;

implementation


uses
  fServerConfig, fServerDataModule, TntSysUtils,SyncObjs, SofitSystemLibrary_Intf,uROClient,DataAbstract4_Intf,uDAInterfaces;

{$R *.dfm}

procedure TServerForm.actAboutExecute(Sender: TObject);
begin
//
end;

procedure TServerForm.actConfigExecute(Sender: TObject);
begin
  fmServerConfig.ShowModal;
end;

procedure TServerForm.actExitExecute(Sender: TObject);
begin
  StopService;
  FCanExit := True;
  Close;
end;

procedure TServerForm.actStartServiceExecute(Sender: TObject);
begin
  StartService;
end;

procedure TServerForm.actStopServiceExecute(Sender: TObject);
begin
  StopService;
end;

procedure TServerForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if not FCanExit then
  begin
    RzTrayIcon1.MinimizeApp;
    Action := caNone;
  end
  else
    Action := caFree;

end;

procedure TServerForm.ShowMenuControl;
begin
  itemStartService.Enabled := not FServiceRunning;
  itemStopService.Enabled := FServiceRunning;
  itemConfig.Enabled := not FServiceRunning;
end;

procedure TServerForm.SpTBXItem1Click(Sender: TObject);
begin
  LoadClientFilesVersion;
end;

procedure TServerForm.StartService;
begin
  ServerDataModule.Server.Active := True;
  FServiceRunning := True;
  ShowMenuControl;
end;

procedure TServerForm.StopService;
begin
  ServerDataModule.Server.Active := False;
  FServiceRunning := False;
  ShowMenuControl;
end;

procedure TServerForm.tmRefreshUserListTimer(Sender: TObject);
var
//  ev: IMessageEvents_Writer;
  i: Integer;
//  userarray: StringArray;
begin
//  ev := (ServerDataModule.ROEventRepository as IMessageEvents_Writer);
//  ev.ExcludeSender := False;
//  userarray := StringArray.Create;
//  try
    UserIDListCs.Acquire;
    try
//      for i := 0 to UserIDList.Count - 1 do
//      begin
//        ev.SessionList.Add(Copy(UserIDList[i], pos('=', UserIDList[i])+1, MaxInt));
//        userarray.Add(UserIDList.Names[i]);
//      end;
      UserClientIDListCs.Acquire;
      try
        if gvCount > 0 then
        begin
          UserClientIDList.Clear;
          UserClientIDList.Text := UserIDList.Text;
        end;
        Inc(gvCount);
      finally
        UserClientIDListCs.Release;
      end;
      UserIDList.Clear;
    finally
      UserIDListCs.Release;
    end;

//  ev.RefreshUserList(EmptyGUID, userarray);
//  lblUserCount.Caption := IntToStr(userarray.Count);
//  finally
//    userarray.Free;
//  end;
end;

function GetYear(Date: TDate): Integer;
var
  y, m, d: WORD;
begin
  DecodeDate(Date, y, m, d);
  Result := y;
end;

function GetMonth(Date: TDate): Integer;
var
  y, m, d: WORD;
begin
  DecodeDate(Date, y, m, d);
  Result := m;
end;

procedure TServerForm.tmWarehouseAnalyseTimer(Sender: TObject);
var
  y,m: Integer;
begin
  y := GetYear(Date);
  m := GetMonth(Date);

  if (y <> FYear) or (m <> FMonth) then
    FIsGenerated := False;

  if not FIsGenerated then
  begin
    GenerateWarehouseAnalyse;
    FYear := y;
    FMonth := m;
    FIsGenerated := True;
  end;
  
end;

procedure TServerForm.LoadClientFilesVersion;
var
  xmlconf: TXmlConf;
begin
  gvApplicationVersion := 0;
  xmlconf := TXmlConf.Create(cApplicationPath + cClientFilesPath + cVersionFile);
  gvApplicationVersion := xmlconf.ReadInteger('application','version',0);
  xmlconf.Free;
end;

procedure TServerForm.RzTrayIcon1LButtonDblClick(Sender: TObject);
begin
  RzTrayIcon1.RestoreApp;
end;

procedure TServerForm.RzTrayIcon1RButtonDown(Sender: TObject);
begin
  RzTrayIcon1.ShowMenu;
end;

procedure TServerForm.GenerateWarehouseAnalyse;
var
  conn: IDAConnection;
  cmd : IDASQLCommand;
  sql: string;
begin
  sql := 'declare @result int exec upc_genwarehousesnapshot '+ IntToStr(GetYear(Date)) + ','+IntToStr(GetMonth(Date))+',@result output select @result';
  conn := ServerDataModule.ConnectionManager.NewConnection('SysConnection');
  cmd := conn.NewCommand(sql,stSql);
  cmd.Execute;
  ShowMenuControl;
end;

procedure TServerForm.FormCreate(Sender: TObject);
begin
  cApplicationPath := WideExtractFilePath(Application.ExeName);
  FCanExit := False;
  FServiceRunning := False;
//  fmServerConfig.ConfFile.ReadString()
  LoadClientFilesVersion;

  FYear := 0;
  FMonth := 0;
  FIsGenerated := False;
  StartService;
end;

procedure TServerForm.itemWarehouseSnapShotClick(Sender: TObject);
begin
//
end;

initialization
  UserClientIDList := TStringList.Create;
  UserClientIDListCs := TCriticalSection.Create;
  UserIdList := TStringList.Create;
  UserIdListCs := TCriticalSection.Create;
finalization
  UserIdListCs.Free;
  UserIdList.Free;
  UserClientIDListCs.Free;
  UserClientIDList.Free;
end.
